package com.sinodata.bsm.cicp.finder.aix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * <p>
 * Description: AIX高可用性发现程序，程序返回Subsystem,Group
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-19 PM 11:03:09   wangpeng       1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class HighAvailable extends AixFinder {
    private static Logger logger = Logger.getLogger(HighAvailable.class);
    private static final String shell = "lssrc -g cluster |awk 'NR>1 {print $0}'";
    private static final String regex = "(\\S+)\\s+(\\S+)\\s+\\d+\\s+.*";

    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        BufferedReader reader = null;
        Pattern pattern = Pattern.compile(regex);
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            String strLine = null;
            while ((strLine = reader.readLine()) != null) {
                strLine = strLine.trim();
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    String subsystem = matcher.group(1);
                    String group = matcher.group(2);
                    String[] res = new String[5];
                    res[0] = subsystem + "_" + group;
                    res[1] = "HA_" + subsystem;
                    res[2] = subsystem + "_" + group;
                    res[3] = subsystem;
                    res[4] = group;
                    list.add(res);
                }
            }
        } catch (IOException e) {
            logger.error("failed to find " + ip + " ha ", e);
            return null;
        }
        return list;
    }
}
